<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Model\ImageNew;
use App\Model\Selfs;
use App\Model\Images;
use App\Model\Time;
use App\Model\Selfentry;
use App\User;
use App\Model\Trajectory;
use App\Model\Demand;
use App\Model\Company;
use App\Model\UnitCancellation;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use DB;

class DemandController extends Controller
{
    public function create(Request $request)
    {
    	$user = Auth::user();
    	$data = $request->all();
    	$demand=$data;
    	$demand['uid']=$user->id;
    	$demand['time']=time();
		$id = Demand::insertGetId($demand);
    	if($id){
    		return response()->json(['message' =>'插入成功','code' => 200], 200);
    	}else{
    		return response()->json(['message' =>'插入失败','code' => 400], 200);
    	} 
    }
    public function list(Request $request)
    {
    	$user = Auth::user();
    	$id=$user->id;
    	$type = $request->input('type');
    	$where = array();
        $where[] = array('type', $type);
        $where[] = array('uid', $id);
		$data= Demand::where($where)->orderby('id','desc')->get()->toarray();
		foreach($data as &$k){
			$k['time']=date("Y-m-d H:i:s",$k['time']);
			$k['reply_time']=date("Y-m-d H:i:s",$k['reply_time']);
			if(!isset($k['reply_content'])){
				$k['reply_content']='无';
			}
			unset($k['uid']);
			unset($k['cid']);
			unset($k['type']);
		}
		return response()->json(['data' => $data,'message'=>'查询成功','code' => 200], 200); 
    }
    
    
     public function unitcreate(Request $request)
    {
    	$user = Auth::user();
    	$data = $request->all();
    	$unit=$data;
    	$unit['apply_userid']=$user->id;
    	$unit['created_at']=date("Y-m-d H:i:s",time());
		$id = UnitCancellation::insertGetId($unit);
		if($id){
    		return response()->json(['message' =>'插入成功','code' => 200], 200);
    	}else{
    		return response()->json(['message' =>'插入失败','code' => 400], 200);
    	}
    }
    public function unitlist(Request $request)
    {
    	$user = Auth::user();
    	$id=$user->id;
        $where[] = array('apply_userid', $id);
		$data= UnitCancellation::where($where)->orderby('id','desc')->get()->toarray();
		foreach($data as &$k){
			$com=Company::where('id',$k['company_id'])->first();
			$k['company_id']=$com['comName'];
			unset($k['apply_userid']);
			$user=User::where('id',$k['reject_userid'])->first();
			$k['reject_userid']=$user['name']?:'暂无';
			$k['reject_reason']=$k['reject_reason']?:'暂无';
			$k['image']=$k['image']?:'无';
			$k['status']=$k['status']?'通过':'驳回';
		}
		return response()->json(['data' => $data,'message'=>'查询成功','code' => 200], 200); 
    }
     public function getCompanyList(Request $request)
    {
        $name = $request->input('comName');
        $where = array();
        $where[] = array('deleted_status',1);
        if(!empty($name)) {
            $where[] = array('comName', 'like', '%'.$name.'%');
        }
        $fields = array("id","comName");
        $companyList = Company::where($where)->select($fields)->paginate(10,false);
        $a=$companyList->items();
        return response()->json(array('data'=>$a,'message'=>'返回公司列表成功','code'=>200),200);
    }
    
}